<template>
  <div id="app">
    <div class="container">
      <!-- 顶部框模块 -->
      <div class="form-group">
        <div class="input-group">
          <h4>品牌管理</h4>
        </div>
      </div>

      <!-- 数据表格 -->
      <table class="table table-bordered table-hover mt-2">
        <thead>
          <tr>
            <th>编号</th>
            <th>资产名称</th>
            <th>价格</th>
            <th>创建时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in list" :key="item.id">
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>

            <!-- 如果价格超过100，就有red这个类 -->
            <td :class="{ red: item.price >= 100 }">{{ item.price }}</td>
            <td>{{ item.time | formatTime }}</td>
            <td><a @click.prevent="delFn(item.id)" href="#">删除</a></td>
          </tr>
          <tr v-show="this.list.length !== 0" style="background-color: #eee">
            <td>统计:</td>
            <td colspan="2">总价钱为:{{ allPrice }}</td>
            <td colspan="2">平均价:{{ avraPrice }}</td>
          </tr>
        </tbody>

        <tfoot v-show="this.list.length === 0">
          <tr>
            <td colspan="5" style="text-align: center">暂无数据</td>
          </tr>
        </tfoot>
      </table>

      <!-- 添加资产 -->
      <form class="form-inline">
        <div class="form-group">
          <div class="input-group">
            <input
              v-model.trim="name"
              type="text"
              class="form-control"
              placeholder="资产名称"
            />
          </div>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <div class="form-group">
          <div class="input-group">
            <input
              v-model.number="price"
              type="text"
              class="form-control"
              placeholder="价格"
            />
          </div>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <!-- 阻止表单提交 -->
        <button @click.prevent="addFn" class="btn btn-primary">添加资产</button>
      </form>
    </div>
  </div>
</template>

<script>
import moment from "moment";
export default {
  //监听list的变化，实现本地存储
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
    },
  },
  methods: {
    //新增
    addFn() {
      if (this.name.length === 0 || this.price <= 0)
        return alert("请输入正确的内容");
      //点击添加资按钮，给数组添加元素
      this.list.push({
        id: +new Date(),
        name: this.name,
        price: this.price,
        time: new Date(),
      });
      this.name = "";
      this.price = "";
    },
    //删除
    delFn(id) {
      console.log(id);
      //方式一：filter
      //filter筛选出符合条件的元素，v-for不能监听到filter的更新数组，要重新赋值
      //this.list = this.list.filter((item) => item.id !== id);
      //方式二：findIndex + splice
      this.list.splice(
        this.list.findIndex((item) => item.id === id),
        1
      );
    },
  },
  //计算总价
  computed: {
    allPrice() {
      //求和 reduce
      return this.list.reduce((sum, item) => (sum += item.price), 0).toFixed(2);
    },
    avraPrice() {
      return (this.allPrice / this.list.length).toFixed(2);
    },
  },
  //过滤时间
  filters: {
    formatTime(time) {
      return moment(time).format("YYYY-MM-DD HH:mm:ss");
    },
  },
  data() {
    return {
      name: "", // 名称
      price: 0, // 价格
      list: JSON.parse(localStorage.getItem("list")) || [],
    };
  },
};
</script>

<style >
.red {
  color: red;
}
</style>